package info.wf0913.blogger_service.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import info.wf0913.blogger_core.service.BaseService;
import info.wf0913.blogger_core.service.imp.BaseServiceImp;
import info.wf0913.blogger_service.bean.Comment;
import info.wf0913.blogger_service.bean.Essay;
import info.wf0913.blogger_service.bean.Label;
import info.wf0913.blogger_service.mapper.EssayMapper;
import info.wf0913.blogger_service.service.CommentService;
import info.wf0913.blogger_service.service.EssayService;
import info.wf0913.blogger_service.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class EssayServiceImp extends BaseServiceImp<Essay> implements EssayService{

    @Autowired
    private CommentService commentService;
    @Autowired
    private LabelService labelService;

    /**
     * 根据标题名标题名相通的信息
     * @param essayHesd 标题名
     * @return
     */

    @Override
    public List<Essay> selectList(String essayHesd) {
        QueryWrapper<Essay> wrapper = new QueryWrapper<>();
        if (essayHesd!=null && !essayHesd.equals("")){
            wrapper.like("essay_head",essayHesd);
        }

        List<Essay> tblEssays = getBaseMapper().selectList(wrapper);

        return tblEssays;
    }

    /**
     * 查询博客的详细内容
     * @param id 博客id
     * @return
     */
    @Override
    public Essay selectOne(Integer id) {

        QueryWrapper<Essay> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Essay essay = getBaseMapper().selectOne(wrapper);
        //查询该博客的标签
        List<Integer> labelIds = ((EssayMapper) getBaseMapper()).getLabelIds(essay.getId());
        ArrayList<Label> labels = new ArrayList<>();
        for (Integer labelId : labelIds) {
            labels.add(labelService.selectOne(labelId));
        }
        essay.setTblLabels(labels);
        //分页查询评论
        List<Integer> commentIds =((EssayMapper)getBaseMapper()).getCommentIds(essay.getId());
        essay.setTblComments(commentService.pages(1,10,commentIds));
        return essay;
    }
}
